package zd.cornermemory.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import zd.cornermemory.R;
import zd.cornermemory.utils.CoordinateUtil;
import zd.cornermemory.utils.SPUtils;
import zd.cornermemory.utils.SpKey;

/**
 * 修改坐标
 */
public class MotifyCoornateActivity extends BaseActivity {

    private Spinner coordinate;
    private String coordinatestr;
    private Button save_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_motify_coornate);
        coordinate = findViewById(R.id.coordinate);

        String[] coordianteArray = getResources().getStringArray(R.array.coordinate);
        List<String> list = new ArrayList<>();
        for(int i=0;i<coordianteArray.length;i++) {
            list.add(coordianteArray[i]);
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,
                R.layout.spinner_checked_text, list);
        coordinate.setAdapter(adapter);
        save_button = findViewById(R.id.save_button);
        initData();
        setBackImage();
    }

    private void initData(){

        final String coordinateStr = SPUtils.getInstance().getString(SpKey.COORDINATE);
        if(TextUtils.isEmpty(coordinateStr)){
            coordinate.setSelection(2);
        }else{
            List<String> flags =  CoordinateUtil.getcoordinateFlag();
            for(int i = 0;i<flags.size();i++) {
                if (TextUtils.equals(coordinateStr, flags.get(i))) {
                    coordinate.setSelection(i);
                    break;
                }
            }
        }

        save_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SPUtils.getInstance().put(SpKey.COORDINATE,coordinatestr);
                String str = "1";
                switch (coordinatestr) {
                    case "15":
                        str = "1";break;
                    case "16":
                        str = "2";break;
                    case "13":
                        str = "3";break;
                    case "14":
                        str = "4";break;
                    case "25":
                        str = "5";break;
                    case "26":
                        str = "6";break;
                    case "23":
                        str = "7";break;
                    case "24":
                        str = "8";break;
                    case "35":
                        str = "9";break;
                    case "36":
                        str = "10";break;
                    case "31":
                        str = "11";break;
                    case "32":
                        str = "12";break;
                    case "45":
                        str = "13";break;
                    case "46":
                        str = "14";break;
                    case "41":
                        str = "15";break;
                    case "42":
                        str = "16";break;
                    case "53":
                        str = "17";break;
                    case "54":
                        str = "18";break;
                    case "51":
                        str = "19";break;
                    case "52":
                        str = "20";break;
                    case "63":
                        str = "21";break;
                    case "64":
                        str = "22";break;
                    case "61":
                        str = "23";break;
                    case "62":
                        str = "24";break;

                }

                SPUtils.getInstance().put(HTMLActivity.HTML_KEY + "codeinput",str);

                Toast.makeText(mContext,getString(R.string.motify_sucs),Toast.LENGTH_SHORT).show();
                Intent broadcast = new Intent(SpKey.MODIFY_COORNIATE_SUCCESS);
                sendBroadcast(broadcast);
                finish();
            }
        });

        coordinate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                List<String> flags = CoordinateUtil.getcoordinateFlag();
                coordinatestr = flags.get(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

}
